package com.alibaba.citrus.cr.unified.inventory.utils;

import com.alibaba.cz.base.tool.result.Result;
import com.alibaba.cz.enterprise.api.model.dictionary.DictionaryQueryRequest;
import com.alibaba.cz.enterprise.api.model.dictionary.DictionaryResponse;
import com.alibaba.cz.enterprise.api.service.DictionaryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: yuhw
 * @date: 2023年12月12日 19:33
 **/
@Component
public class DictionaryConverter {

    @DubboReference(protocol = "dubbo")
    DictionaryService dictionaryService;


    public List<DictionaryResponse> getDictionaryByGroup(String group){
        DictionaryQueryRequest request = new DictionaryQueryRequest();
        request.setGroup(group);

        Result<List<DictionaryResponse>> dictionaries = dictionaryService.getDictionarys(request);
        if (dictionaries.isSuccess()) {
            return dictionaries.getResult();
        }
        return null;
    }

    public Boolean checkDictionaryCode(String group,String code){
        if(org.apache.commons.lang3.StringUtils.isBlank(code)){
            return Boolean.FALSE;
        }
        List<com.alibaba.cz.enterprise.api.model.dictionary.DictionaryResponse> dictionaryByGroup = getDictionaryByGroup(group);
        if (CollectionUtils.isEmpty(dictionaryByGroup)){
            return false;
        }
        Set<String> codeSet = dictionaryByGroup.stream()
                .map(com.alibaba.cz.enterprise.api.model.dictionary.DictionaryResponse::getCode).collect(Collectors.toSet());
        return codeSet.contains(code);
    }
}
